package com.mywebsite.Service.Impl;


import com.mywebsite.Dao.UserDao;
import com.mywebsite.Domain.User;
import com.mywebsite.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;


    public User getById(Integer id){
        User user =  userDao.selectById(id);
        System.out.println(user);
        return user;
    }

    public User getByName(String Username){
        User user =  userDao.getByName(Username);
        System.out.println(user);
        return user;
    }

    public Integer register(String username, String password, String useremail) {
        //获取时间
        Calendar cal = Calendar.getInstance();
        Date date=new Date();
        cal.setTime(date);
        Integer year=cal.get(Calendar.YEAR);
        Integer month = cal.get(Calendar.MONTH)+1;
        Integer day=cal.get(Calendar.DAY_OF_MONTH);
        String createtime = year+"年"+month+"月"+day+"日";

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setCreatetime(createtime);
        user.setUseremail(useremail);
        Integer affected = userDao.insert(user);
        return affected;
    }

    @Override
    public Integer update(String username, String newusername,String password, String useremail, String userinfo) {
        User user1 =  userDao.getByName(username);
        Integer id = user1.getId();
        User user = new User();
        user.setId(2);
        user.setUsername(newusername);
        user.setPassword(password);
        user.setUseremail(useremail);
        user.setUserinfo(userinfo);
        userDao.update(user);
        return 1;
    }


}
